package com.example.nonacademic_education.util.timer;

import com.example.nonacademic_education.dao.SqlBackupsDao;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author xinmiao
 * @description
 * @date 2023/3/26 17:01
 */
@Slf4j
public class MysqlBackups implements Job {
    @Resource
    private SqlBackupsDao sqlBackupsDao;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置日期格式
        String formattedTime = sdf.format(new Date(System.currentTimeMillis()));
        String preUrl = "https://fxljy.cwnu.edu.cn:80/yw/downloadSqlFile?path=backups/";
        String filename="backups_" + formattedTime+".sql";
        sqlBackupsDao.addOne(filename,preUrl + filename);
//        String savePath="/home/fxljy/mysql/backups/";
//        String hostIP="localhost";
//        String username="online";
//        String password="Pwd123--";
//        String fileName="backups";
//        LocalDate currentDate = LocalDate.now();
//        int year = currentDate.getYear();
//        int month = currentDate.getMonthValue();
//        int day = currentDate.getDayOfMonth();
//        fileName=fileName+"-"+year+"-"+month+"-"+day+".sql";
//        String databaseName="non-academic";
//        File saveFile = new File(savePath);
//        if (!saveFile.exists()) {// 如果目录不存在
//            saveFile.mkdirs();// 创建文件夹
//        }
//        if(!savePath.endsWith(File.separator)){
//            savePath = savePath + File.separator;
//        }
//        //File sqlFile=new File(saveFile+fileName);
//        PrintWriter printWriter = null;
//        BufferedReader bufferedReader = null;
//        try {
//            printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));
//            //log.debug("is avv?");
//            Process process = Runtime.getRuntime().exec(" mysqldump -h" + hostIP + " -u" + username + " -p" + password + " --set-charset=UTF8 " + databaseName);
//            InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
//            bufferedReader = new BufferedReader(inputStreamReader);
//            String line;
//            while((line = bufferedReader.readLine())!= null){
//                printWriter.println(line);
//            }
//            printWriter.flush();
//            if(process.waitFor() == 0){//0 表示线程正常终止。
//                log.error("database backups success");
//            }
//        }catch (IOException | InterruptedException e) {
//            e.printStackTrace();
//            log.error("database backups error");
//        } finally {
//            try {
//                if (bufferedReader != null) {
//                    bufferedReader.close();
//                }
//                if (printWriter != null) {
//                    printWriter.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        log.error("database backups error");
    }
}
